﻿## HERO
class PmEngine
    constructor:(@options) ->
        container = @options.container
        stage = new Kinetic.Stage
            container:@options.container
            width: parseInt (($("##{container}").css 'width').replace 'px', '')
            height: parseInt (($("##{container}").css 'width').replace 'px', '')
        @layer = new Kinetic.Layer
        stage.add @layer
        @processes = []      
    
    addProcess:(processModel) ->
        process = undefined
        if processModel.name is 'base'
            process = new ProcessStandard
                model:processModel                
                x:50
                y:5
                width:100
                height:50
                layer:@layer
        @processes.push process 
        @layer.add process.Me

        @draw()
    draw:-> 
        @layer.draw()

class ProcessBase extends UserControl
    constructor:(@attrs) ->
        @attrs.draggable = true
        super @attrs
        @parent = @attrs.parent
        @children = []
        @readonly = @attrs.readonly

        @panel = new DockPanel
            x:@getX()
            y:@getY()
            width:@width
            height:@height
            layer:@layer
            draggable:@attrs.draggable
            dragConstraint: "horizontal"
            
        @group.add @panel.Me

        @leftAnchor = new Kinetic.Rect            
            width:5
            height:@height
            fill:'whitesmoke' 
            stroke:'violet'
            strokeWidth:1                
        @leftAnchor.dock = 'left'
        @panel.addChild @leftAnchor

        @rightAnchor = new Kinetic.Rect            
            width:5
            height:@height
            fill:'whitesmoke' 
            stroke:'violet'
            strokeWidth:1                  
        @rightAnchor.dock = 'right'
        @panel.addChild @rightAnchor

        @rect = new Kinetic.Rect            
            width:20
            height:@height
            fill:'whitesmoke' 
            stroke:'violet'
            strokeWidth:1                
        @rect.dock = 'fill'
        @panel.addChild @rect

        resizing = false

        resizer = undefined

        @leftAnchor.on 'mousedown', =>
            @panel.group.draggable false
            resizing = true;
            resizer = @leftAnchor
        @rightAnchor.on 'mousedown', =>
            @panel.group.draggable false
            resizing = true;
            resizer = @rightAnchor        

        canvas = $ @layer.canvas                
        
        canvas.mouseup (evt)=>
            resizing = false
            @panel.group.draggable true

        
        canvas.mousemove (evt)=>
            if resizing isnt true
                return

            bound = @panel.getBound()

            if resizing is true and resizer is @leftAnchor and evt.offsetX < bound.right()
                deltaW = bound.x - evt.offsetX                
                @panel.setX evt.offsetX
                @panel.setWidth (deltaW + @panel.width)
                @layer.draw()
            if resizing is true and resizer is @rightAnchor and evt.offsetX > bound.x
                @panel.setWidth (evt.offsetX - bound.x)
                @layer.draw()

    
    parent: (@parent)
    getParent: -> @parent
    setParent: (@parent) ->
    from: (@from)
    setFrom: (@from)
    getFrom: ->@from
    to: (@to)
    getTo: -> @to
    setTo:(@to)->

class ProcessStandard extends UserControl
    constructor:(@attrs)->
        super @attrs
        b = @getBound()
        @panel = new AutoVerticalOrderPanel
            x:b.x
            y:b.y
            width:b.w
            height:b.h
            layer:@layer
        rect = new Kinetic.Rect            
            width:100
            height:30
            x:0
            fill:'white' 
            stroke:'violet'
            strokeWidth:1  
        @panel.addChild rect

        rect = new Kinetic.Rect            
            width:100
            height:30
            x:160
            fill:'red' 
            stroke:'violet'
            strokeWidth:1  
            
        @panel.addChild rect

        rect = new Kinetic.Rect            
            width:100
            height:30
            x:100
            fill:'yellow' 
            stroke:'violet'
            strokeWidth:1  
        @panel.addChild rect 
        @group.add @panel.Me       